package com.bci.pwtz.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bci.pwtz.admin.service.AdminCompanyService;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.CompanyMapper;
import com.bci.pwtz.mysql.model.Company;

public class AdminCompanyServiceImpl implements AdminCompanyService {

	private static final Logger _logger = Logger.getLogger(AdminCompanyServiceImpl.class);
	
	private CompanyMapper companyMapper;
	
	public void setCompanyMapper(CompanyMapper companyMapper) {
		this.companyMapper = companyMapper;
	}

	@Override
	public PaginationList<Company> findCompany(Company company, String beginTime,
    		String endTime,int pageNo, int pageSize) throws PwtzDatabaseException {
		
		try {
			int beginIndex = pageSize * pageNo - pageSize;
			List<Company> list = companyMapper.findCompanyList(company, beginTime, endTime, beginIndex, pageSize);
			_logger.debug("AdminCompanyServiceImpl.findCompany() result list = "+list);
			int totalCount = companyMapper.findCompanyCount(company, beginTime, endTime);
			_logger.debug("AdminCompanyServiceImpl.findCompany() result totalCount = "+totalCount);
			int totalPage = (totalCount + pageSize -1)/pageSize;
			
			PaginationList<Company> paginationList = new PaginationList<Company>();
			paginationList.setTotalPage(totalPage);
			paginationList.setCurrPage(pageNo);
			paginationList.setPageSize(pageSize);
			paginationList.setRecords(list);
			paginationList.setTotalRecord(totalCount);
			return paginationList;
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}
	@Override
	public List<Company> findCompany(Company company, String beginTime,String endTime) throws PwtzDatabaseException{
		try {
			List<Company> list = companyMapper.findCompanyList(company, beginTime, endTime, 0, 0);
			_logger.debug("AdminCompanyServiceImpl.findCompany() result list = "+list);
			return list;
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public int approvePay(Long id,Date invalidTime) throws PwtzDatabaseException {
		try {
			return companyMapper.approvePay(id, invalidTime);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
	public Company load(Long id) throws PwtzDatabaseException {
		try {
			return companyMapper.selectCompanyInfo(id);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
	}

	@Override
    public List<Company> selectcCompanyStatistics(String provinceCode, String cityCode, String beginTime, String endTime) throws PwtzDatabaseException
    {
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectcCompanyStatistics:|provinceCode=" + provinceCode+"|cityCode="+cityCode+"|beginTime="+beginTime+"|endTime="+endTime);
        }
        List<Company> list = new ArrayList<Company>();
        try
        {
            list = companyMapper.selectcCompanyStatistics(provinceCode, cityCode, beginTime, endTime);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return list;
    }

}
